Passed
Push — master ( c4d61c...59bf76 )
by Kolja
01:06
created

jgfContainer.js ➔ graphs   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 1
dl 0
loc 7
rs 10
nop 0
1
const { JGFGraph } = require('./jgfGraph');
2
3
/**
4
 * JGF Container (main class) of zero or more JGF graphs
5
 */
6
class JGFContainer {
7
8
    /**
9
     * Constructor
10
     * @param {*} singleGraph true for single-graph mode, false for multi-graph mode
11
     */
12
    constructor(singleGraph = true) {
13
        this._graphs = [];
14
        this.isSingleGraph = singleGraph;
15
16
        if (singleGraph) {
17
            this.addEmptyGraph();
18
        }
19
    }
20
21
    /**
22
     * Returns all graphs, in Multi-Graph mode
23
     */
24
    get graphs() {
25
        if (this.isSingleGraph) {
26
            throw new Error('Cannot call graphs() in Single-Graph mode')
27
        }
28
29
        return this._graphs;
30
    }
31
32
    /**
33
     * Returns the graph, in Single-Graph mode
34
     */
35
    get graph() {
36
        if (!this.isSingleGraph) {
37
            throw new Error('Cannot call graph() in Multi-Graph mode')
38
        }
39
40
        return this._graphs[0];
41
    }
42
43
    /**
44
     * Returns true if the container is in Multi-Graph mode
45
     */
46
    get isMultiGraph() {
47
        return !this.isSingleGraph;
48
    }
49
50
    /**
51
     * Adds an empty graph
52
     */
53
    addEmptyGraph() {
54
        let graph = new JGFGraph();
55
        this._graphs.push(graph);
56
57
        return graph;
58
    }
59
}
60
61
module.exports = {
62
    JGFContainer,
63
};